package cn.iocoder.yudao.module.promotion.service.channel;

import cn.iocoder.yudao.module.promotion.controller.admin.channel.vo.*;
import cn.iocoder.yudao.module.promotion.controller.app.channel.vo.ChannelLinkReqVO;
import cn.iocoder.yudao.module.promotion.convert.channel.ChannelLinkConvert;
import cn.iocoder.yudao.module.promotion.dal.dataobject.channel.ChannelLinkDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.channel.ChannelLinkMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.CHANNEL_LINK_NOT_EXISTS;

/**
 * 商城链接配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ChannelLinkServiceImpl implements ChannelLinkService {

    @Resource
    private ChannelLinkMapper channelLinkMapper;

    @Override
    public Long createChannelLink(ChannelLinkCreateReqVO createReqVO) {
        // 插入
        ChannelLinkDO channelLink = ChannelLinkConvert.INSTANCE.convert(createReqVO);
        channelLink.setUrl(channelLink.getDomain() + "?points=" + channelLink.getPoints() + "&economizePrice=" + channelLink.getEconomizePrice() + "&preferentialRatio=" + channelLink.getPreferentialRatio());
        channelLinkMapper.insert(channelLink);
        // 返回
        return channelLink.getId();
    }

    @Override
    public void updateChannelLink(ChannelLinkUpdateReqVO updateReqVO) {
        // 校验存在
        validateChannelLinkExists(updateReqVO.getId());
        // 更新
        ChannelLinkDO updateObj = ChannelLinkConvert.INSTANCE.convert(updateReqVO);
        updateObj.setUrl(updateObj.getDomain() + "?points=" + updateObj.getPoints() + "&economizePrice=" + updateObj.getEconomizePrice());
        channelLinkMapper.updateById(updateObj);
    }

    @Override
    public void deleteChannelLink(Long id) {
        // 校验存在
        validateChannelLinkExists(id);
        // 删除
        channelLinkMapper.deleteById(id);
    }

    private void validateChannelLinkExists(Long id) {
        if (channelLinkMapper.selectById(id) == null) {
            throw exception(CHANNEL_LINK_NOT_EXISTS);
        }
    }

    @Override
    public ChannelLinkDO getChannelLink(Long id) {
        return channelLinkMapper.selectById(id);
    }

    @Override
    public List<ChannelLinkDO> getChannelLinkList(Collection<Long> ids) {
        return channelLinkMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ChannelLinkDO> getChannelLinkPage(ChannelLinkPageReqVO pageReqVO) {
        return channelLinkMapper.selectStatisticsPage(pageReqVO);
    }

    @Override
    public List<ChannelLinkDO> getChannelLinkList(ChannelLinkExportReqVO exportReqVO) {
        return channelLinkMapper.selectList(exportReqVO);
    }

    @Override
    public void closeChannelLink(Long id) {
        // 校验存在
        validateChannelLinkExists(id);
        // 更新
        ChannelLinkDO updateObj = new ChannelLinkDO().setId(id).setStatus((byte) 1);
        channelLinkMapper.updateById(updateObj);
    }

    @Override
    public ChannelLinkDO getDetail(ChannelLinkReqVO linkVO) {
        return channelLinkMapper.getDetail(linkVO);
    }
}
